/**
 * 
 * @class NG.response.NCDDServices
 * @extends NG.response.Response
 */
NG.response.NCDDServices = Ext.extend(NG.response.Response, {

	resultDataList: [],
	
	/**
	 * parses given data and creates {NG.models.Field} objects according to data
	 * 
	 * @param fieldsData
	 * @returns {Array} {NG.models.Field} array
	 */
	parseFields : function(fieldsData) {

		if (Ext.isEmpty(fieldsData))
			return [];
		
		var fieldList = [];
		
		if(!Ext.isDefined(fieldsData.Field)) {
			return fieldList;
		}
		
		for (var i=0; i<fieldsData.Field.length; ++i)
		{
			fieldList.push(
				new NG.models.Field({
			        fieldName: fieldsData.Field[i].FieldName,
			        displayName: fieldsData.Field[i].DisplayName,
			        fieldType: fieldsData.Field[i].FieldType,
			        isKey: fieldsData.Field[i].IsKey,
			        mimeType: fieldsData.Field[i].MimeType,
			        isLookUp: fieldsData.Field[i].IsLookUp,
			        lookUpProps: fieldsData.Field[i].LookUpProps
				})
			);
		}
		return fieldList;
	},
	
	/**
	 * Obtains {NG.models.TableLayout} object from given data
	 * 
	 * @param {String }layoutsData
	 * @returns {NG.models.TableLayout}
	 * 
	 * Called by {@link NG.response.NCDDServices.parseTable}
	 */
	parseLayouts : function(layoutsData) {
		
		return new NG.models.TableLayout({
	        layoutName: layoutsData.LayoutName,
	        viewType: layoutsData.ViewType,
	        layoutFields: this.parseFields(layoutsData.Fields)
	    });
	},
	
	/**
	 * Obtains {NG.models.Table} objects from given data
	 * 
	 * @param tableData
	 * @returns {NG.models.Table}
	 * 
	 * Called by {@link NG.response.NCDDServices.parseXMLData}
	 */
	parseTable: function(tableData) {
		 return new NG.models.Table({
	          tableName: tableData.TableName,
	          tableDisplayName: tableData.TableDisplayName,
	          tableVisibility: tableData.TableVisibility,
	          datasetType: tableData.DatasetType,
	          isGeoTable: tableData.IsGeoTable,
	          fieldList: this.parseFields(tableData.Fields),
	          layoutList: this.parseLayouts(tableData.Layouts)
		 });
	},
	
	/**
	 * Parses given xml data and obtains NCDD tables <br>
	 * NOTE: this class can only parse GET_TABLE related request responses <br>
	 * GET_XSL requests are not handled<br>
	 * Called by {@link NG.response.NCDDServices.init} 
	 * 
	 * @param {String} xmlData xml formated string.
	 * 
	 */
	parseXMLData: function (xmlData){
		var xmlStr = fleegix.xml.parse(fleegix.xml.createDoc(xmlData), 'string').string;
		var id = 0;
		var tableArray = fleegix.xml.parse(fleegix.xml.createDoc(xmlStr), "Tables").Tables;
		if (Ext.isDefined(tableArray.Table))
		{
			for (var i = 0 ; i<tableArray.Table.length; i++) {
				this.resultDataList.push({
			        id: id++,
			        element: this.parseTable(tableArray.Table[i])
				});
			}
		}
	},
	
	/**
	 * Array of object, used by Ext.List store
	 * @returns {Array} NG.models.Table
	 */
	getResultList: function (){
		var result = [];
		for ( var i = 0; i < this.resultDataList.length; i++) {
			result.push(this.resultDataList[i].element);
		}
		return result;
	},
	
	/**
	 * Used by GetInfoEx as FAD list
	 * @returns {Array}
	 */
	getResultTableList: function (){
		var resultList = this.getResultList();
		var resultTableList = [];
		
		for (var i=0; i<resultList.length; ++i) {
			resultTableList.push(resultList[i].data.tableName);
		}
		
		return resultTableList;
	},
	
	/**
	 * returns table data requested by its name
	 * 
	 * @param table name
	 */
	getTable: function (tableName){
		var resultList = this.getResultList();
		var resultTableList = [];
		
		for (var i=0; i<resultList.length; ++i) {
			if( resultList[i].data.tableName == tableName) {
				return resultList[i].data.tableName;
			}
		}
		
		return null;
	},
	
	constructor: function(config){
		NG.response.NCDDServices.superclass.constructor.call(this, config);
	},
	
	/**
	 * Parses given xmlData (response data) <br>
	 * Fulfills {NG.response.NCDDServices} members according to parsed xml data
	 * 
	 * @param {String} xmlData - response data
	 * Called by {@link NG.response.Response.constructor}
	 */
	init: function (xmlData) {
		NG.response.NCDDServices.superclass.init.call(this, xmlData);
		this.parseXMLData(xmlData);
	}
});
